package work.xiaohong.arithmetic.IO;

import lombok.Cleanup;
import work.xiaohong.arithmetic.entity.User;

import java.io.*;

/**
 * @author xiaohong
 * @version 1.0
 * @date 2021/10/27 0027 16:38
 * @description 转换流
 * 可将字节流转换为字符流 可设置字符的编码方式
 */
public class InputStreamReaderAndOutputStreamWriter {

    public static void main(String[] args) throws Exception {
//        writeFile();
        readFile();
//        copyFile();
    }

    // 写入文件
    public static void writeFile() throws Exception {
        // 后面的参数表示是否要子在原来的内容后面追加新内容
        FileOutputStream fileOutputStream = new FileOutputStream("a.txt",true);
        @Cleanup
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"gbk");
        outputStreamWriter.write("你好世界");
        outputStreamWriter.flush();
    }


    // 读取文件
    public static void readFile() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("a.txt");
        @Cleanup
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"gbk");

        int data = 0;
        while ((data = inputStreamReader.read()) != -1){
            System.out.print((char) data);
        }

    }


}
